package com.example.AIstudy.service.impl;
import java.sql.Time;
import java.text.DecimalFormat;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.AIstudy.DTO.LevelRecordDTO;
import com.example.AIstudy.dao.LevelRecordDao;
import com.example.AIstudy.domain.LevelRecord;
import com.example.AIstudy.domain.Question;
import com.example.AIstudy.exception.AIstudyException;
import com.example.AIstudy.service.LevelRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Calendar;
@Service
public class LevelRecordServiceImpl extends ServiceImpl<LevelRecordDao,LevelRecord> implements LevelRecordService {

    @Autowired
    LevelRecordDao levelRecordDao;

    @Override
    public void insertLevelRecord(LevelRecord levelRecord) throws AIstudyException {
        try{
            levelRecordDao.insertLevelRecord(levelRecord);
        }catch (Exception e){
            throw new AIstudyException(AIstudyException.ErrorType.INSERT_ERROR,"添加学习水平周记录失败");
        }
    }

    @Override
    public IPage<LevelRecord> findRecords(Map<String, Object> condition, Integer pageNum, Integer pageSize) {
        IPage page = new Page(pageNum, pageSize);
        QueryWrapper<LevelRecord> qw = new QueryWrapper<>();
        qw.eq(condition.containsKey("stuID"), "stuID", condition.get("stuID"));
        //学生查询问题信息的时候可以筛选
        qw.like(condition.containsKey("level"), "overlevel", condition.get("level"));
        qw.ge(condition.containsKey("startTime"), "startTime", condition.get("startTime"));
        qw.le(condition.containsKey("endTime"), "endTime", condition.get("endTime"));
        return levelRecordDao.findRecords(page, qw);
    }
    public LevelRecordDTO findRecordDetail(Integer stuID){

        List<LevelRecord>records=levelRecordDao.getRecordByTime(stuID);
        int size=records.size();
        LevelRecordDTO recordDTO= new LevelRecordDTO();
        //获取上周的评价
        if(size>0){
            LevelRecord record=records.get(0);
            //进行封装
            recordDTO.setChoicePerformance(record.getChoicePerformance());
            recordDTO.setChoiceSuggestion(record.getChoiceSuggestion());
            recordDTO.setEssayPerformance(record.getEssayPerformance());
            recordDTO.setEssaySuggestion(record.getEssaySuggestion());
            recordDTO.setTranslationPerformance(record.getTranslationPerformance());
            recordDTO.setTranslationSuggestion(record.getTranslationSuggestion());
            recordDTO.setReadingPerformance(record.getReadingPerformance());
            recordDTO.setReadingSuggestion(record.getReadingSuggestion());
            recordDTO.setLanKnowledgeComment(record.getLanKnowledgeComment());
            recordDTO.setLanCompreScore(record.getLanCompreScore());
            recordDTO.setLanKnowledgeScore(record.getLanKnowledgeScore());
            recordDTO.setLanCompreComment(record.getLanCompreComment());
            recordDTO.setLanExpreScore(record.getLanExpreScore());
            recordDTO.setLanExpreComment(record.getLanExpreComment());
        };
        //封装list
        //六周的记录
        List<Float> overLevels=new ArrayList<>();
        //六周的评价
        List<String> suggestions=new ArrayList<>();
        //做题的准确率，依次是fanyi、xuanze、yuedu和zuowen
        List<Float>correctRate=new ArrayList<>();
        //获取水平和建议并封装
        for(int i=size-1;i>=0;i--){
            LevelRecord tmp=records.get(i);
            overLevels.add(tmp.getOverlevel());
            suggestions.add(tmp.getOverEvaluation());
        }
        recordDTO.setOverLevels(overLevels);
        recordDTO.setSuggestions(suggestions);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY); // 设置为本周的周一
        cal.add(Calendar.DATE, -7); // 回退一周，即得到上周一的日期
        cal.set(Calendar.HOUR_OF_DAY, 0); // 设置为零点
        cal.set(Calendar.MINUTE, 0); // 设置为零分
        cal.set(Calendar.SECOND, 0); // 设置为零秒
        cal.set(Calendar.MILLISECOND, 0); // 设置为零毫秒
        long timestamp = cal.getTimeInMillis(); // 获取时间戳

//        Calendar cal2 = Calendar.getInstance();
//        cal2.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY); // 设置为本周的周日
        cal.add(Calendar.DATE, 6); // 回退一周，即得到上周日的日期
        cal.set(Calendar.HOUR_OF_DAY, 23); // 设置为23点
        cal.set(Calendar.MINUTE, 59); // 设置为59分
        cal.set(Calendar.SECOND, 59); // 设置为59秒
        cal.set(Calendar.MILLISECOND, 999); // 设置为999毫秒

        long timestamp2 = cal.getTimeInMillis(); // 获取时间戳
        //上周一零点
        Timestamp startTime=new Timestamp(timestamp);
        //上周一最后一秒
        Timestamp endTime=new Timestamp(timestamp2);
        //获取学情分析，每种题目的准确率
        List<Float>list1=levelRecordDao.getCorrectRateIncuoti(stuID,startTime,endTime);
        List<Float>list2=levelRecordDao.getCorrectRateInExe(stuID,startTime,endTime);
        List<Float>list3=levelRecordDao.getCorrectRateInRedo(stuID,startTime,endTime);
        for(int i=0;i<4;i++){
            float num1=0;
            float num2=0;
            float num3=0;
            //查询到的结果可能为空
            if(list1.get(i)==null){
                num1=0;
            }else{
                num1=list1.get(i);
            }
            if(list2.get(i)==null){
                num2=0;
            }else{
                num2=list2.get(i);
            }
            if(list3.get(i)==null){
                num3=0;
            }else{
                num3=list3.get(i);
            }
            DecimalFormat df = new DecimalFormat("#.##");
            float result=(num1+num2+num3)/3;
//            if(i==0)result= 0.9F;
//            else if(i==1)result=0.32F;
//            else if(i==2)result=0.43F;
//            else result=0.78F;
            // 使用 format 方法将 double 类型的数值格式化
            correctRate.add(Float.valueOf(df.format(result)));
        }
        recordDTO.setCorrectRate(correctRate);
//        correctRate.add(new Float(0.9));

        return recordDTO;
    }
}
